﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Setup_Pst_Backup_2013
{
    static class Program
    {
        private struct Arguments
        {
            internal frmSetup.Actions DeploymentAction { get; set; }
            internal bool IsSilent { get; set; }
        }

        /// <summary>
        /// Point d'entrée principal de l'application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Arguments currentArgs = GetArguments(args);

            if (IsOutlook2013Installed(currentArgs.IsSilent))
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                frmSetup setup = new frmSetup(currentArgs.DeploymentAction, currentArgs.IsSilent);
                setup.PerformeDeployment();
            }
            else
            {
                if (!currentArgs.IsSilent)
                    MessageBox.Show("Can't find Outlook 2013 on this computer.");
            }
        }

        private static Arguments GetArguments(string[] args)
        {
            Arguments arguments = new Arguments();
            arguments.DeploymentAction = frmSetup.Actions.Install;
            arguments.IsSilent = false;

            foreach (string arg in args)
            {
                string loweredArg = arg.ToLower();
                switch (loweredArg)
                {
                    case "/action=install":
                    case "-action=install":
                    case "action=install":
                    case "install":
                        arguments.DeploymentAction = frmSetup.Actions.Install;
                        break;
                    case "/action=uninstall":
                    case "-action=uninstall":
                    case "action=uninstall":
                        arguments.DeploymentAction = frmSetup.Actions.Uninstall;
                        break;
                    case "/s":
                    case "-s":
                    case "s":
                    case "/silent":
                    case "-silent":
                    case "silent":
                        arguments.IsSilent = true;
                        break;
                    case "/?":
                    case "-?":
                    case "?":
                        MessageBox.Show("Allow to Install or Uninstall 'Pst Backup 2013'\r\n - /Action=Install : to install the software\r\n - /Action=Uninstall : to uninstall the software\r\n - /s : to performe a silent installation/uninstallation");
                        break;
                    default:
                        break;
                }
            }

            return arguments;
        }

        private static bool IsOutlook2013Installed(bool silent)
        {
            try
            {
                Microsoft.Win32.RegistryKey baseKey64 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Office\15.0\Outlook\InstallRoot", false);
                Microsoft.Win32.RegistryKey baseKey32 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Office\15.0\Outlook\InstallRoot", false);

                if (baseKey64 != null)
                {
                    string pathValue = string.Empty;
                    pathValue = (string)baseKey64.GetValue("Path");
                    if (!string.IsNullOrEmpty(pathValue))
                        return true;
                }
                else
                    if (baseKey32 != null)
                    {
                        string pathValue = string.Empty;
                        pathValue = (string)baseKey32.GetValue("Path");
                        if (!string.IsNullOrEmpty(pathValue))
                            return true;
                    }
            }
            catch (Exception ex)
            {
                if (!silent)
                    MessageBox.Show(ex.Message);
            }

            return false;
        }
    }
}
